#region License, Terms and Conditions
//
// Prawks - Copyright (c) 2007 Atif Aziz. All rights reserved.
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation; either version 2.1 of the License, or (at your option)
// any later version.
//
// This library is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
// details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this library; if not, write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
//
#endregion

namespace Prawks.CodeProvisioning
{
    using System;

    using PropertyInfo = System.Reflection.PropertyInfo;

    /// <summary>
    /// Allows a type to externally provide the implementation for a property.
    /// </summary>

    public interface IPropertyMethodImpl
    {
        /// <summary>
        /// Gets the property to which the implementation is bound.
        /// </summary>

        PropertyInfo Property { get; }
    }

    /// <summary>
    /// When overriden in a derived class, allows a type to be designated as
    /// the external implementation provider for a property.
    /// </summary>

    [ AttributeUsage(AttributeTargets.Property) ]
    public abstract class PropertyImplAttribute : MemberImplAttribute
    {
        /// <summary>
        /// Creates the implementation provider for the get method of a property 
        /// to which the attribute is applied.
        /// </summary>
        /// <param name="property">
        /// The property whose get implementation provider is to be supplied.
        /// </param>
        /// <returns>
        /// Returns the implementation provider for the property's get method.
        /// </returns>

        public virtual IMethodImpl CreateGetImpl(PropertyInfo property)
        {
            Type implType = InferImplType("GetImpl");

            if (implType == null)
            {
                throw new MemberImplException(
                    string.Format("{0} has no corresponding implementation provider type. Override and supply an implementation for CreateGetImpl on {0}.",
                    GetType().FullName));
            }

            return (IMethodImpl) Activator.CreateInstance(implType, new object[] { property });
        }

        /// <summary>
        /// Creates the implementation provider for the set method of a property 
        /// to which the attribute is applied.
        /// </summary>
        /// <param name="property">
        /// The property whose set implementation provider is to be supplied.
        /// </param>
        /// <returns>
        /// Returns the implementation provider for the property's set method.
        /// </returns>

        public virtual IMethodImpl CreateSetImpl(PropertyInfo property)
        { 
            Type implType = InferImplType("SetImpl");

            if (implType == null)
            {
                throw new MemberImplException(
                    string.Format("{0} has no corresponding implementation provider type. Override and supply an implementation for CreateSetImpl on {0}.",
                    GetType().FullName));
            }

            return (IMethodImpl) Activator.CreateInstance(implType, new object[] { property });
        }
    }
}
